package pers.baiban.sqltoword.util;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 表格样式集中获取工具类
 * <p>
 * table层级是
 * table -> row -> col -> cell -> paragraph
 * 任意表格层级 -> cttc -> tcpr
 *
 * @author 强子初
 * @since 2024/2/1
 */
public class TableUtil {

    /**
     * 获取表头的cell
     */
    public static List<XWPFTableCell> getHeader(XWPFTable table) {
        return table.getRow(0).getTableCells();
    }

    /**
     * 转换器方法
     *
     * @param list     待转换集合
     * @param function 转换方法
     * @return 转换完成后的集合
     */
    public static <T, R> List<R> to(List<T> list, Function<T, R> function) {
        return list.stream().map(function).collect(Collectors.toList());
    }

    /**
     * 转换器方法
     *
     * @param source   待转换对象
     * @param function 转换方法
     * @return 转换完成后的对象
     */
    public static <T, R> R to(T source, Function<T, R> function) {
        return function.apply(source);
    }


    /**
     * 转换函子
     * <br/>
     * 函子间拓扑关系
     * <pre>
     *
     *         → paragraph → run
     *      ↗
     * cell → cttc → tcpr → shd
     *                    ↘
     *                       → borders → border
     * <pre/>
     */
    public static class Functors {

        /**
         * 根据 {@link XWPFTableCell} 获取 {@link CTTc}
         */
        public static final Function<XWPFTableCell, CTTc> CTTC =
                XWPFTableCell::getCTTc;

        /**
         * 根据 {@link XWPFTableCell} 获取 {@link XWPFParagraph}
         */
        public static final Function<XWPFTableCell, XWPFParagraph> PARAGRAPH =
                cell -> cell.getParagraphArray(0);

        /**
         * 根据 {@link XWPFParagraph} 获取 {@link XWPFRun}
         */
        public static final Function<XWPFParagraph, XWPFRun> RUN =
                paragraph -> paragraph.getRuns().get(0);

        /**
         * 根据 {@link CTTc} 获取 {@link CTTcPr}
         */
        public static final Function<CTTc, CTTcPr> TCPR =
                cttc -> cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr();

        /**
         * 根据 {@link CTTcPr} 获取 {@link CTShd}
         */
        public static final Function<CTTcPr, CTShd> SHD =
                tcpr -> tcpr.isSetShd() ? tcpr.getShd() : tcpr.addNewShd();

        /**
         * 根据 {@link CTTcPr} 获取 {@link CTTcBorders}
         */
        public static final Function<CTTcPr, CTTcBorders> BORDERS =
                tcPr -> tcPr.isSetTcBorders() ? tcPr.getTcBorders() : tcPr.addNewTcBorders();

        /**
         * 根据 {@link CTTcBorders} 获取 {@link CTBorder} 下边框
         */
        public static final Function<CTTcBorders, CTBorder> BORDER_BOTTOM =
                borders -> borders.isSetBottom() ? borders.getBottom() : borders.addNewBottom();

        public static final Function<CTTcBorders, CTBorder> BORDER_INSIDEH =
                borders -> borders.isSetInsideH() ? borders.getInsideH() : borders.addNewInsideH();
    }
}